package cn.edu.scau.cmi.xiaowenyang.hibernate;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

import cn.edu.scau.cmi.xiaowenyang.hibernate.dao.StudentDAO;
import cn.edu.scau.cmi.xiaowenyang.hibernate.dao.TeacherDAO;
import cn.edu.scau.cmi.xiaowenyang.hibernate.domain.Student;
import cn.edu.scau.cmi.xiaowenyang.hibernate.domain.Teacher;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class HibernateController implements Initializable {
	@FXML
	private TextField addStudentText;
	@FXML
	private ChoiceBox deleteStudentBox;
	@FXML
	private Button addStudentButton;
	@FXML
	private Button deleteStudentButton;
	@FXML
	private Button updateStudentButton;
	@FXML
	private ChoiceBox updateStudentBox;
	@FXML
	private TextField updateStudentText;
	@FXML
	private ChoiceBox choseTutorStudentBox;
	@FXML
	private ChoiceBox choseTutorTeacherBox;
	@FXML
	private Button choseTutorButton;
	@FXML
	private Button showTutorButton;
	@FXML
	private TextArea showTutorText;
	@FXML
	private ChoiceBox showTutorBox;
	@FXML
	private TextField addTeacherText;
	@FXML
	private ChoiceBox deleteTeacherBox;
	@FXML
	private Button addTeacherButton;
	@FXML
	private Button deleteTeacherButton;
	@FXML
	private Button updateTeacherButton;
	@FXML
	private ChoiceBox updateTeacherBox;
	@FXML
	private TextField updateTeacherText;
	@FXML
	private ChoiceBox choseTeacherBox;
	@FXML
	private ChoiceBox choseStudentBox;
	@FXML
	private Button choseAddButton;
	@FXML
	private Button choseDeleteButton;
	@FXML
	private TextArea showStudentText;
	@FXML
	private ChoiceBox showStudentBox;
	@FXML
	private Button showStudentButton;
	private StudentDAO studentDAO;
	private TeacherDAO teacherDAO;
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		// TODO Auto-generated method stub
		studentDAO = new StudentDAO();
		teacherDAO = new TeacherDAO();
		reflush();
		addStudentButton.setOnMouseClicked(e -> {
			Student student = new Student();
			student.setName(addStudentText.getText());
			studentDAO.save(student);
			addStudentText.clear();
			showTutorText.clear();
			reflush();
		});

		deleteStudentButton.setOnMouseClicked(e -> {
			studentDAO.delete((Student) deleteStudentBox.getValue());
			showTutorText.clear();
			reflush();
		});

		updateStudentButton.setOnMouseClicked(e -> {
			Student student = (Student) updateStudentBox.getValue();
			student.setName(updateStudentText.getText());
			studentDAO.update(student);
			updateStudentText.clear();
			showTutorText.clear();
			reflush();
		});

		choseTutorButton.setOnMouseClicked(e -> {
			Student student = (Student) choseTutorStudentBox.getValue();
			Teacher teacher = (Teacher) choseTutorTeacherBox.getValue();
			student.setTeacher(teacher);
			studentDAO.update(student);
			showTutorText.clear();
			reflush();
		});

		showTutorButton.setOnMouseClicked(e -> {
			Student student = (Student) showTutorBox.getValue();
			if (student.getTeacher() == null) {
				showTutorText.setText("该生尚未选择指导老师");
			} else {
				showTutorText.setText(student.getTeacher().getName());
			}
		});

		addTeacherButton.setOnMouseClicked(e -> {
			Teacher teacher = new Teacher();
			teacher.setName(addTeacherText.getText());
			teacherDAO.save(teacher);
			addTeacherText.clear();
			showStudentText.clear();
			reflush();
		});

		deleteTeacherButton.setOnMouseClicked(e -> {
			teacherDAO.delete((Teacher) deleteTeacherBox.getValue());
			showStudentText.clear();
			reflush();
		});

		updateTeacherButton.setOnMouseClicked(e -> {
			Teacher teacher = (Teacher) updateTeacherBox.getValue();
			teacher.setName(updateTeacherText.getText());
			teacherDAO.update(teacher);
			updateTeacherText.clear();
			showStudentText.clear();
			reflush();
		});

		choseAddButton.setOnMouseClicked(e -> {
			Student student = (Student) choseStudentBox.getValue();
			Teacher teacher = (Teacher) choseTeacherBox.getValue();
			student.setTeacher(teacher);
			studentDAO.update(student);
			showStudentText.clear();
			reflush();
		});

		choseDeleteButton.setOnMouseClicked(e -> {
			Student student = (Student) choseStudentBox.getValue();
			Teacher teacher = (Teacher) choseTeacherBox.getValue();
			if (student.getTeacher().getId().equals(teacher.getId())) {
				student.setTeacher(null);
				studentDAO.update(student);
			}
			showStudentText.clear();
			reflush();
		});

		showStudentButton.setOnMouseClicked(e -> {
			Teacher teacher = (Teacher) showStudentBox.getValue();
			List<Student> list = teacherDAO.findAllStudents(teacher);
			showStudentText.clear();
			for (Student it : list) {
				showStudentText.setText(showStudentText.getText() + it.getName() + "\n");
			}
		});

	}

	public void reflush() {
		List students = studentDAO.findAll();
		List teachers = teacherDAO.findAll();
		deleteStudentBox.setItems(FXCollections.observableArrayList(students));
		updateStudentBox.setItems(FXCollections.observableArrayList(students));
		choseTutorStudentBox.setItems(FXCollections.observableArrayList(students));
		choseTutorTeacherBox.setItems(FXCollections.observableArrayList(teachers));
		deleteTeacherBox.setItems(FXCollections.observableArrayList(teachers));
		updateTeacherBox.setItems(FXCollections.observableArrayList(teachers));
		choseTeacherBox.setItems(FXCollections.observableArrayList(teachers));
		choseStudentBox.setItems(FXCollections.observableArrayList(students));
		showStudentBox.setItems(FXCollections.observableArrayList(teachers));
		showTutorBox.setItems(FXCollections.observableArrayList(students));
	}
}
